package com.malou.mventas.client.login;

import java.util.HashMap;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.malou.mventas.client.Models;

public class Logins extends Models {
	private static LoginServiceAsync ls = GWT.create(LoginService.class);
	private static HashMap<String, Object> param_report;
	public static void validar_user_3(String user, String pw){
		
		ls.isValidUser(user, pw, new AsyncCallback<String>() {
			
			@Override
			public void onSuccess(String result) {				
				validar_user_4(result);
			}
			
			@Override
			public void onFailure(Throwable caught) {				
				//show_error();
			}
		});
		
		
		
	}
	
	public static void issetUser(){
		ls.issetUser(new AsyncCallback<String>() {

			@Override
			public void onFailure(Throwable caught) {
				//show_error();				
			}

			@Override
			public void onSuccess(String result) {
				if(result != null)
					isset_user(result);
				else
					start();
			}
		});
	}
	
	public static void logout_user(){
		ls.logout(new AsyncCallback<Void>() {

			@Override
			public void onFailure(Throwable caught) {
				//show_error();				
			}

			@Override
			public void onSuccess(Void result) {			
				
			}
		});
	}
	
	public static void HtmlReportViewer(String fileName, ReportParam param, int consulta_id){		
				
		param_report = new HashMap<String, Object>(); 
		param_report.put("tienda_id", param.getTienda_id());
		param_report.put("tienda", param.getTienda());		
		param_report.put("desde", param.getDesde());
		param_report.put("hasta", param.getHasta());
		
		ls.getHtmlReport(fileName, param_report, consulta_id, new AsyncCallback<String>() {

			@Override
			public void onFailure(Throwable caught) {				
				//show_error();				
			}

			@Override
			public void onSuccess(String result) {
				
				if(result != null){
					doIframe(result);
				}else{
					show_alert("No se pudo cargar el Reporte");
				}
					
			}

		});
		
		
	}
	
	public static void getBalanceTienda(String fecha, int tienda_id){
		ls.getBalanceTienda(fecha, tienda_id, new AsyncCallback<String>() {

			@Override
			public void onFailure(Throwable caught) {
				//show_error();
			}

			@Override
			public void onSuccess(String result) {
				if(result != null){
					doBalance(result);
				}else{
					show_alert("No se pudo cargar el Reporte");
				}
			}
		});
	}
	
	public static native void validar_user_4(String result)/*-{
		$wnd.validar_user_5(result);
	}-*/;
	
	public static native void create_validar_user_2()/*-{
		$wnd.validar_user_2 = function(user,pw){
			@com.malou.mventas.client.login.Logins::validar_user_3(Ljava/lang/String;Ljava/lang/String;)(user,pw);
		};		
	}-*/;
	
	private static native void start()/*-{
		$wnd.start();
	}-*/;
	
	public static native void isset_user(String result)/*-{
		$wnd.isset_user(result);
	}-*/;
	
	public static native void create_logout()/*-{
		$wnd.logout = function(){
			@com.malou.mventas.client.login.Logins::logout_user()();
		};		
	}-*/;
	
	public static native void create_htmlReportViewer()/*-{
		$wnd.HtmlReportViewer = function(fileName, param, consulta_id){
			@com.malou.mventas.client.login.Logins::HtmlReportViewer(Ljava/lang/String;Lcom/malou/mventas/client/login/ReportParam;I)(fileName, param, consulta_id);
		};
	}-*/;
	public static native void doIframe(String url)/*-{
		$wnd.doIframe(url);
	}-*/;	
	
	public static native void create_getBalanceTienda()/*-{
		$wnd.getBalanceTienda = function(fecha,tienda_id){
			@com.malou.mventas.client.login.Logins::getBalanceTienda(Ljava/lang/String;I)(fecha, tienda_id);
		};
	}-*/;
	
	public static native void doBalance(String data)/*-{
		$wnd.doBalance(data);
	}-*/;
}
